package com.xl.rentkeeper.adapter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xl.fuse.protos.viewroom.RoomInfo;
import com.xl.fuse.protos.viewroom.ViewOrderStatus;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.business.OrderLogic;
import com.xl.rentkeeper.entity.Order;
import com.xl.rentkeeper.fragment.PendingFragment;
import com.xl.rentkeeper.util.DialogUtil;
import com.xl.rentkeeper.util.TimeUtils;
import com.xl.rentkeeper.util.TipMapping;
import com.xl.rentkeeper.util.Util;

import java.util.List;

/**
 * Created by Administrator on 2015/8/22.
 */
public class OrderAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Activity mActivity;
    List<Order> list;
    private DialogUtil.DialogCallback dialogCallback;
    public OrderAdapter(Activity mActivity, LayoutInflater inflater, List<Order> list) {
        this.inflater = inflater;
        this.list = list;
        this.mActivity = mActivity;
    }

    public void setDialogCallback(DialogUtil.DialogCallback callback){
        this.dialogCallback = callback;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final Order order = list.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.order_list_item, null, false);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tag = (TextView) convertView.findViewById(R.id.tv_tag);
            holder.time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_action = (TextView) convertView.findViewById(R.id.tv_action);
            holder.roomInfo1 = (TextView) convertView.findViewById(R.id.tv_roominfo1);
            holder.tv_roominfo_status = (TextView) convertView.findViewById(R.id.tv_roominfo_status);
            holder.roomPrice1 = (TextView) convertView.findViewById(R.id.tv_price1);
            holder.state = (TextView) convertView.findViewById(R.id.tv_state);
            holder.isShowRoom1 = (LinearLayout) convertView.findViewById(R.id.ll_roomInfo1);
            holder.ll_content = (LinearLayout) convertView.findViewById(R.id.ll_content);
            holder.ll_viewtime = (LinearLayout) convertView.findViewById(R.id.ll_viewtime);
            // holder.orderId = (TextView) convertView.findViewById(R.id.order_id);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //  holder.orderId.setVisibility(View.GONE);
        if (order.tag != null) {
            holder.tag.setText(order.tag);
            holder.tag.setVisibility(View.VISIBLE);
            holder.ll_content.setVisibility(View.GONE);
        } else {
            holder.tag.setVisibility(View.GONE);
            holder.ll_content.setVisibility(View.VISIBLE);
            holder.name.setText(order.viewOrder.name);
            holder.time.setText(Util.replaceTimeNull(order.viewOrder.viewTime));
            if(order.viewOrder.status == ViewOrderStatus.ORDER_IS_SIGN.getValue()){
                holder.roomInfo1.setText(Util.replaceNull(null, ""));
                holder.roomPrice1.setText("");
                holder.tv_roominfo_status.setText(Util.replaceNull(null, ""));
            }else{
                if (order.viewOrder.viewRooms != null && order.viewOrder.viewRooms.size() >= 1) {
                    RoomInfo rInfo = order.viewOrder.viewRooms.get(0);
                    holder.roomInfo1.setText(Util.replaceNull(null, rInfo.name));
                    Util.setRoomStatus(holder.tv_roominfo_status, rInfo, "list");
                    Util.setRoomPrice(holder.roomPrice1,rInfo,"list");
                } else {
                    holder.roomInfo1.setText(Util.replaceNull(null, ""));
                    holder.roomPrice1.setText("");
                    holder.tv_roominfo_status.setText(Util.replaceNull(null, ""));
                }
            }
            holder.state.setText("");
            holder.tv_action.setText(R.string.contact);
            if (order.status == ViewOrderStatus.ORDER_NOT_RECEIVE.getValue()) {
                holder.tv_action.setText(R.string.receiver_order);
                holder.tv_action.setBackgroundResource(R.drawable.btn_receiver_order);
                if(!TextUtils.isEmpty(order.timeoutLength)){
                    holder.state.setVisibility(View.VISIBLE);
                    holder.state.setText(order.timeoutLength);
                }  else {
                    holder.state.setVisibility(View.GONE);
                }
            } else if (order.status == ViewOrderStatus.ORDER_IS_RECEIVE.getValue()) {
                holder.tv_action.setBackgroundResource(R.drawable.btn_callphone);
                if(!TextUtils.isEmpty(order.timeoutLength)){
                    holder.state.setVisibility(View.VISIBLE);
                    holder.state.setText(order.timeoutLength);
                } else {
                    holder.state.setVisibility(View.GONE);
                }
            } else if (order.status == ViewOrderStatus.ORDER_IS_SIGN.getValue()) {
                holder.tv_action.setBackgroundResource(R.drawable.btn_callphone);
                holder.roomInfo1.setVisibility(View.GONE);
                holder.roomPrice1.setVisibility(View.GONE);
                holder.tv_roominfo_status.setVisibility(View.GONE);
            } else if (order.status == ViewOrderStatus.ORDER_NOT_SIGN.getValue()) {
                holder.tv_action.setBackgroundResource(R.drawable.btn_callphone);
                holder.roomInfo1.setVisibility(View.GONE);
                holder.roomPrice1.setVisibility(View.GONE);
                holder.tv_roominfo_status.setVisibility(View.GONE);
            } else if (order.status == ViewOrderStatus.ORDER_NOT_VIEW.getValue()) {
                holder.tv_action.setBackgroundResource(R.drawable.btn_callphone);
                holder.roomInfo1.setVisibility(View.GONE);
                holder.roomPrice1.setVisibility(View.GONE);
                holder.tv_roominfo_status.setVisibility(View.GONE);
            } else if (order.status == ViewOrderStatus.ORDER_IS_VIEW.getValue()) {
                holder.state.setVisibility(View.GONE);
                if(TimeUtils.getTodayStart()>order.viewOrder.viewTime){
                    holder.time.setTextColor(mActivity.getResources().getColor(R.color.red));
                }else{
                    holder.time.setTextColor(mActivity.getResources().getColor(R.color.txt_04));
                }
                holder.tv_action.setBackgroundResource(R.drawable.btn_callphone);
            }

            if (order.status != ViewOrderStatus.ORDER_NOT_RECEIVE.getValue()) {
                holder.tv_action.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Uri uri = Uri.parse("tel:" + order.viewOrder.phone);
                        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                        mActivity.startActivity(intent);
                    }
                });
            } else {
                holder.tv_action.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtil.showMsgInfoDialog(mActivity, new DialogUtil.DismissClickListener() {
                            @Override
                            public void okClick() {
                                OrderLogic.getInstance().stewardOrder(order.viewOrder.id, PendingFragment.reqContext);//
                            }

                            @Override
                            public void cancelClick() {

                            }
                        }, "这个客户我来搞定", "确认接单", "确定", "还是算了");
                    }
                });
            }
        }
        return convertView;
    }

    class ViewHolder {
        TextView name;
        TextView tag;
        TextView time;
        TextView roomInfo1;//房间信息
        TextView roomPrice1;//房间价格
        TextView state;//状态(超时,未联系)
        TextView tv_action;
        TextView tv_roominfo_status;
        LinearLayout isShowRoom1;
        LinearLayout ll_content;
        LinearLayout ll_viewtime;
    }

}
